package com.ztg.analogbrowser.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.Arrays;
import java.util.Set;

public class AnalogBrowserGetCookie {
    private static final Log log = LogFactory.get();

    public static Set<Cookie> getCookie(String url) throws Exception {
        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        //        要排除的参数，可以是多个（Arrays.asList("enable-automation","--disable-sync")）
        options.setExperimentalOption("excludeSwitches", Arrays.asList("enable-automation"));
        //--headless不显示浏览器
        //        options.addArguments("--headless");
        ChromeDriver driver = new ChromeDriver(options);
        // 访问登陆页面
        driver.get(url);
        for (int i = 0; i < 5; i++) {
            log.info("等待{}s", 5 - i);
            Thread.sleep(1000);
        }
        log.info("开始获取cookies…………");
        Set<Cookie> cookies = driver.manage().getCookies();
        log.info("结束获取cookies");
        driver.close();
        driver.quit();
        return cookies;
    }
}
